/*

 */
package com.ohos.connent.bizscenecloud.biz.modular.dp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connent.bizscenecloud.biz.modular.dp.entity.BizDpProduct;
import com.ohos.connent.bizscenecloud.biz.modular.dp.enums.StatusEnum;
import com.ohos.connent.bizscenecloud.biz.modular.dp.param.*;
import com.ohos.connent.bizscenecloud.biz.modular.dp.result.BizDpProductDetailResult;
import com.ohos.connent.bizscenecloud.biz.modular.dp.result.BizDpProductPageResult;
import com.ohos.connent.bizscenecloud.biz.modular.dp.result.BizProCategoryDetailResult;
import com.ohos.connent.bizscenecloud.biz.modular.dp.service.BizDpProductService;
import com.ohos.connent.bizscenecloud.common.annotation.CommonLog;
import com.ohos.connent.bizscenecloud.common.exception.CommonException;
import com.ohos.connent.bizscenecloud.common.pojo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 产品开发控制器
 **/
@Api(tags = "产品开发控制器")
@ApiSupport(author = "OHOSCONNECT_TEAM", order = 7)
@RestController
@RequestMapping(value = "/api/v1/business/biz/dp/product")
@Validated
public class BizDpProductController {

    @Resource
    private BizDpProductService bizDpProductService;

    /**
     * 产品开发列表
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("产品开发列表")
    @GetMapping("/list")
    public CommonResult<Page<BizDpProduct>> list(BizDpProductPageParam bizDpProductPageParam) {

        return CommonResult.data(bizDpProductService.listByPage(bizDpProductPageParam));
    }

    /**
     * 获取品类树
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("获取品类树")
    @PostMapping("/tree")
    public CommonResult<List<Tree<String>>> tree(@RequestBody @Valid BizDpProductTreeParam bizProCategoryDictTreeParam) {
        return CommonResult.data(bizDpProductService.tree(bizProCategoryDictTreeParam));
    }

    /**
     * 复制产品
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("复制产品")
    @CommonLog("复制产品")
    @GetMapping("/copy")
    public CommonResult<String> copy(@Valid BizDpProductIdParam bizDpProductIdParam) {
        bizDpProductService.copy(bizDpProductIdParam);
        return CommonResult.ok();
    }

    /**
     * 新增产品
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("新增产品")
    @CommonLog("新增产品")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid BizDpProductAddParam bizDpProductAddParam) {
        bizDpProductService.add(bizDpProductAddParam);
        return CommonResult.ok();
    }

    /**
     * 修改产品
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("修改产品")
    @CommonLog("修改产品")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizDpProductEditParam bizDpProductEditParam) {
        bizDpProductService.edit(bizDpProductEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除产品
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("删除产品")
    @CommonLog("删除产品")
    @GetMapping("/delete")
    public CommonResult<String> delete(@Valid BizDpProductIdParam bizDpProductIdParam) {
        bizDpProductService.delete(bizDpProductIdParam);
        return CommonResult.ok();
    }

    /**
     * 产品详情
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("查看产品详情")
    @GetMapping("/detail")
    public CommonResult<BizDpProductDetailResult> detail(@Valid BizDpProductIdParam bizDpProductIdParam) {
        BizDpProduct detail = bizDpProductService.detail(bizDpProductIdParam);
        // 处理行业字段
        BizDpProductDetailResult bizDpProductDetailResult = BeanUtil.copyProperties(detail,BizDpProductDetailResult.class);
        Map<String,Tree> result = bizDpProductService.getTreeList(new BizDpProductTreeParam());
        List<String> categoryList = new ArrayList<>();
        Tree tree = result.get(detail.getCategoryDictParentId());
        if (ObjectUtils.isNotEmpty(tree)){
            Tree parentTree = result.get(tree.getParentId().toString());
            if (ObjectUtils.isNotEmpty(parentTree)) {
                bizDpProductDetailResult.setCategory(parentTree.getName() + "/" + tree.getName());
                categoryList.add(parentTree.getId().toString());
            }
            categoryList.add(tree.getId().toString());
        }
        categoryList.add(detail.getCategoryDictId());
        bizDpProductDetailResult.setCategoryList(categoryList);
        BizProCategoryDetailResult bizProCategoryDetailResult = bizDpProductService.getCategoryDictDetail(detail.getCategoryDictId());
        if (!Objects.isNull(bizProCategoryDetailResult)) {
            bizDpProductDetailResult.setProFileId(bizProCategoryDetailResult.getFileId());
        }
        return CommonResult.data(bizDpProductDetailResult);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("获取品类ICON文件id")
    @CommonLog("获取品类ICON文件id")
    @PostMapping("/geFileUploadIconId")
    public CommonResult<String> geFileUploadIconId(@RequestPart("file") MultipartFile file) throws IOException {
        bizDpProductService.geFileUploadIconId(file);
        return CommonResult.ok();
    }

    /**
     * 提交审核
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation("提交审核")
    @CommonLog("提交审核")
    @PostMapping("/submit")
    public CommonResult<String> submit(@RequestBody BizDpProductIdParam bizDpProductIdParam) {
        bizDpProductService.submit(bizDpProductIdParam);
        return CommonResult.ok();
    }

    /**
     * 提交审核
     */

    @ApiOperationSupport(order = 10)
    @ApiOperation("撤回审核")
    @CommonLog("撤回审核")
    @PostMapping("/withdraw")
    public CommonResult<String> withdraw(@RequestBody BizDpProductIdParam bizDpProductIdParam) {
        bizDpProductService.withdraw(bizDpProductIdParam);
        return CommonResult.ok();
    }


    /**
     * 产品开发审核列表
     */
    @ApiOperationSupport(order = 11)
    @ApiOperation("产品开发审核列表")
    @GetMapping("/page")
    public CommonResult<Page<BizDpProductPageResult>> page(BizDpProductPageParam bizDpProductPageParam) {
        Page<BizDpProduct> page = bizDpProductService.page(bizDpProductPageParam);
        // 处理行业字段
        Page<BizDpProductPageResult> resultPage = new Page<>();
        resultPage.setCurrent(page.getCurrent());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());
        resultPage.setPages(page.getPages());
        List<BizDpProductPageResult> results = new ArrayList<>();
        Map<String,Tree> result = bizDpProductService.getTreeList(new BizDpProductTreeParam());
        page.getRecords().forEach(bizDpProduct -> {
            BizDpProductPageResult bizDpProductPageResult = BeanUtil.toBean(bizDpProduct,BizDpProductPageResult.class);
            Tree tree = result.get(bizDpProduct.getCategoryDictParentId());
            if (ObjectUtils.isNotEmpty(tree)){
                Tree parentTree = result.get(tree.getParentId().toString());
                if (ObjectUtils.isNotEmpty(parentTree)) {
                    bizDpProductPageResult.setCategory(parentTree.getName() + "/" + tree.getName());
                }
            }
            if (ObjectUtils.isNotEmpty(StatusEnum.getByKey(bizDpProduct.getStatus()))) {
                bizDpProductPageResult.setStatus(StatusEnum.getByKey(bizDpProduct.getStatus()).getValue());
            }
            results.add(bizDpProductPageResult);
        });
        resultPage.setRecords(results);
        return CommonResult.data(resultPage);
    }

    /**
     * 产品开发审核
     */
    @ApiOperationSupport(order = 12)
    @ApiOperation("产品开发审核")
    @CommonLog("产品开发审核")
    @PostMapping("/examine")
    public CommonResult<String> examine(@RequestBody BizDpProductExamineParam bizDpProductExamineParam) {
        bizDpProductService.examine(bizDpProductExamineParam);
        return CommonResult.ok();
    }

    /**
     * 产品开发审核列表
     */
    @ApiOperationSupport(order = 13)
    @ApiOperation("详情页面审核列表")
    @GetMapping("/examineList")
    public CommonResult<List<String>> examineList(@Valid BizDpProductIdParam bizDpProductIdParam) {
        return CommonResult.data(bizDpProductService.examineList(bizDpProductIdParam));
    }

    /**
     * 配置文件下载
     */
    @ApiOperationSupport(order = 14)
    @ApiOperation("配置文件下载")
    @GetMapping(value= "/profileDownload" ,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void profileDownload(@Valid BizDpProductIdParam bizDpProductIdParam, HttpServletResponse response) {
        BizDpProduct bizDpProduct = bizDpProductService.getById(bizDpProductIdParam.getId());
        if (Objects.isNull(bizDpProduct)) {
            throw new CommonException("当前产品不存在");
        }
        bizDpProductService.profileDownload(bizDpProduct,response);


    }

    /**
     * 产品详情
     */
    @ApiOperationSupport(order = 15)
    @ApiOperation("根据podId查看产品详情")
    @GetMapping("/get")
    public CommonResult<BizDpProductDetailResult> get(@Valid BizDpProductProdIdParam bizDpProductProdIdParam) {
        BizDpProduct detail = bizDpProductService.getByProdId(bizDpProductProdIdParam);
        // 处理行业字段
        BizDpProductDetailResult bizDpProductDetailResult = BeanUtil.copyProperties(detail,BizDpProductDetailResult.class);
        Map<String,Tree> result = bizDpProductService.getTreeList(new BizDpProductTreeParam());
        Tree tree = result.get(detail.getCategoryDictParentId());
        if (ObjectUtils.isNotEmpty(tree)){
            Tree parentTree = result.get(tree.getParentId().toString());
            if (ObjectUtils.isNotEmpty(parentTree)) {
                bizDpProductDetailResult.setCategory(parentTree.getName() + "/" + tree.getName());
            }
        }
        BizProCategoryDetailResult bizProCategoryDetailResult = bizDpProductService.getCategoryDictDetail(detail.getCategoryDictId());
        if (!Objects.isNull(bizProCategoryDetailResult)) {
            bizDpProductDetailResult.setProFileId(bizProCategoryDetailResult.getFileId());
        }
        return CommonResult.data(bizDpProductDetailResult);
    }

}
